module Twilolia
  class Base
    extend Forwardable
    
    def_delegators :client,:get,:post,:put,:delete
    attr_reader :client
    def initialize(client)
      @client= client
    end
    
    #===================== timeline resource begin=============================
    #Optional:since_id,max_id,count,page,trim_user,include_entities
    def home_timeline(query={})
      perform_get("/statuses/home_timeline.json",:query => query)
    end
    
    #Optional:since_id,max_id,count,page,trim_user,include_rts,include_entities
    def friends_timeline(query={})
      perform_get("/statuses/friends_timeline.json",:query => query)
    end
    
    #auth:false
    #Optional:user_id,screen_name,since_id,max_id,count,page,trim_user,include_rts,include_entities
    def user_timeline(query={})
      perform_get("/statuses/user_timeline.json",:query => query)
    end
    
    #tweets that contain @yourname
    #Optional:since_id,max_id,count,page,trim_user,include_rts,include_entities
    def mentions(query={})
      perform_get("/statuses/mentions.json",:query => query)
    end
    
    #only contains offical retweet
    #Optional:since_id,max_id,count,page,trim_user,include_entities
    def retweeted_by_me(query={})
      perform_get("/statuses/retweeted_by_me", :query => query)
    end
    
    #Options:since_id,max_id,count,page,trim_user,include_entities
    def retweeted_to_me(query={})
      perform_get("/statuses/retweeted_to_me", :query => query)
    end
    
    #Options:since_id,max_id,count,page,trim_user,include_entities
    def retweet_of_me(query={})
      perform_get("/statuses/retweets_of_me", :query => query)
    end
    #===================== timeline resource end===============================
    
    #===================== tweets resource begin===============================
    #Optional:trim_user,include_entities
    def tweet(tweet_id,query={})
      perform_get("/statuses/show/#{tweet_id}.json",:query => query)
    end
    
    #create tweet
    #Options:in_reply_to_status_id,lat,long,place_id,display_coordinates,trim_user,include_entities
    def create_tweet(status,query={})
      perform_post("/statuses/update.json",:body => {:status => status}.merge(query))
    end
    
    #delete tweet
    def delete_tweet(tweet_id)
      perform_delete("/statuses/destroy/#{tweet_id}.json")
    end
    
    #retweet a tweet
    def retweet_tweet(tweet_id)
      perform_post("/statuses/retweet/#{tweet_id}.json")
    end
    
    #retweets list
    #Options:count,trim_user,include_entities 
    def offical_retweets(tweet_id,query={})
      Twilolia.offical_retweets(tweet_id,query)
    end
    
    #get users list that retweeted the tweet
    #Options:count,page,trim_user,include_entities
    def users_retweeted(tweet_id,query={})
      perform_get("/statuses/#{tweet_id}/retweeted_by.json",:query => query)
    end
    
    #get users's id list that retweeted the tweet
    #Option:count,page,trime_user,include_entities
    def users_id_retweeted(tweet_id,query={})
      perform_get("/statuses/#{tweet_id}/retweeted_by/ids.json",:query => {}.merge(query))
    end
    #===================== tweets resource end=================================
    
    #===================== user resource begin=================================
    #show user details
    #Options:include_entities
    def user(id,query={})
      Twilolia.user(id,query)
    end
    
    #lookup users screen_name=name1,name2
    #Options:include_entities
    def lookup_users(user_ids,screen_names,query={})
      perform_get("/users/lookup.json",:query => {:user_id => user_ids,:screen_name => screen_names}.merge(query))
    end
    
    #search user
    #Options:per_page,page,include_entities
    def search_users(q,query={})
      perform_get("/users/search.json",:query => {:q => q}.merge(query))
    end
    
    #following people list
    #Options:user_id,screen_name,cursor,include_entities
    def following(id_or_name,query={})
      Twilolia.following(id_or_name,query)
    end
    
    def followers(id_or_name,query={})
      Twilolia.followers(id_or_name,query)
    end
    #===================== user resource end===================================
    
    #===================== list resource begin=================================
    #Options:mode,description
    def add_list(screen_name,list_name,options={})
      perform_post("/#{screen_name}/lists.json",:body => {:name => list_name}.merge(options))
    end
    
    #Options:mode,description
    def rename_list(screen_name,list_old_name,list_new_name,options={})
      perform_post("/#{screen_name}/lists/#{list_old_name}.json",:body => {:name => list_new_name}.merge(options))
    end
    
    #Options:cursor
    def lists(screen_name,query={})
      perform_get("/#{screen_name}/lists.json", :query => query)
    end
    
    def list(screen_name,list_name,query={})
      perform_get("/#{screen_name}/lists/#{list_name}", :query => query)      
    end
    
    def delete_list(screen_name,list_name,options={})
      perform_delete("/#{screen_name}/lists/#{list_name}")
    end
    
    def list_timeline(screen_name,list_name,query={})
      Twilolia.list_timeline(screen_name,list_name,query)
    end
    
    #Options:cursor
    def lists_user_added_to(screen_name,query={})
      perform_get("/#{screen_name}/lists/memberships.json",:query => query)
    end
    
    def lists_user_follows(screen_name,query={})
      perform_get("/#{screen_name}/lists/subscriptions.json",:query => query)
    end
    #===================== list resource end===================================
    
    #===================== list member resource begin==========================
    #return the members of the specified list
    #Options:cursor,include_entities
    def members_in_list(screen_name,list_name,query={})
      perform_get("/#{screen_name}/#{list_name}/members.json",:query => query)
    end
    
    #add a member to a list,you'd better use members_add_by_list
    #require:list_id,id
    def add_member_to_list(screen_name,list_name,new_id,options={})
      perform_post("/#{screen_name}/#{list_name}/members.json", :body => {:id => new_id}.merge(options))
    end
    
    #recommanded!
    #Options:user_id,screen_name (string seperated with english comma)
    def add_members_to_list(screen_name,list_name,options={})
      perform_post("/#{screen_name}/#{list_name}/create_all.json",:body => options)
    end
    
    def delete_member_in_list(screen_name,list_name,id)
      perform_delete("/#{screen_name}/#{list_name}/members.json",:query => {:id => id})
    end
    
    #check if a user is a member of the specified list
    def check_member_is_in_list(screen_name,list_id,member_name)
      perform_get("/#{screen_name}/#{list_id}/members/#{member_name}.json")
    end
    #===================== list member resource end============================
    
    #===================== list subscribers resource begin=====================
    #TO-DO
    #===================== list subscribers resource end=======================
    
    #===================== direct messages resource begin======================
    #TO-DO
    #===================== direct messages resource end========================
    
    #===================== friendship resource begin===========================
    #TO-DO
    #===================== friendship resource end=============================
    
    #===================== friends and followers resource begin================
    #TO-DO
    #===================== friends and followers resource end==================
    
    #===================== account resource begin==============================
    #return 200 if auhentication is successful,401 if not
    def verify_credentials
      perform_get("/account/verify_credentials.json")
    end
    
    def rate_limit_status
      perform_get("/account/rate_limit_status.json")
    end
    
    def end_session
      perform_post("/account/end_session.json")
    end
    #TO-DO
    #===================== account resource end================================
    
    #===================== favorites resource begin============================
    #TO-DO
    #===================== favorites resource end==============================
    
    #===================== notification resource begin=========================
    #TO-DO
    #===================== notification resource end===========================
    
    #===================== block resource begin================================
    #TO-DO
    #===================== block resource end==================================
    
    #===================== spam reporting resource begin=======================
    #TO-DO
    #===================== spam reporting resource end=========================
    
    
    private 
    def perform_get(path, options={})
      Twilolia::Request.get(self,path,options)
    end
    
    def perform_post(path,options={})
      Twilolia::Request.post(self,path,options)
    end
    
    def perform_put(path,options={})
      Twilolia::Request.put(self,path,options)
    end
    
    def perform_delete(path,optinos={})
      Twilolia::Request.delete(self,path,options)
    end
  end
end